﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lcs.Web.project
{
    public partial class listsj : Web.Admin.BasePage
    {
        public string dogid = "NO";
        public string Decode(string str)
        {

            if (dogid == "NO")
            {
                if (Request.Cookies["dogid"] != null)
                    dogid = Request.Cookies["dogid"].Value.ToString();
            }
            return DecodeDog(str, dogid);

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
                Page.DataBind();
            }

        }
        protected void btnout_Click(object sender, EventArgs e)
        {
            DataSet ds = GetData();
            GridView2.DataSource = ds;

            GridView2.AllowPaging = false;
            //GridView2.Columns[0].Visible = false;
            GridView2.DataBind();
            string f = DateTime.Now.Ticks.ToString();
            ExportExcel(GridView2, f);
            GridView2.DataSource = ds;
            GridView2.AllowPaging = true;
            //GridView2.Columns[0].Visible = true;
            GridView2.DataBind();

        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            //这里没有内容。
        }

        public void ExportExcel(System.Web.UI.WebControls.WebControl objControl, string strFileName)
        {

            strFileName = System.Web.HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8);
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.Buffer = true;
            System.Web.HttpContext.Current.Response.Charset = "gb2312";
            System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "online; filename=" + strFileName + ".xls");
            System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";

            System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("zh-CN", true);
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

            objControl.RenderControl(oHtmlTextWriter);

            objControl = null;
            System.Web.HttpContext.Current.Response.Write("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=gb2312\"/>" + oStringWriter.ToString().Replace("<td", "<td STYLE='MSO-NUMBER-FORMAT:\\@'"));
            System.Web.HttpContext.Current.Response.Buffer = false;
            System.Web.HttpContext.Current.Response.End();
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }
        private DataSet GetData()
        {
            DataSet ds = null;
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                int projectId = (Convert.ToInt32(Request.Params["id"]));

                Maticsoft.BLL.project bll = new Maticsoft.BLL.project();
                Maticsoft.Model.project model = bll.GetModel(projectId);
                string projectName = model.projectName;
                lbl_projectname.Text = projectName;
                StringBuilder strWhere = new StringBuilder();
                if (txtKeyword.Text.Trim() != "")
                {
                    strWhere.AppendFormat(" material like '%{0}%'", txtKeyword.Text.Trim());
                }
                if (strWhere.ToString() != "")
                    strWhere.Append(string.Format("and use_projectname='{0}'", projectName));
                else
                    strWhere.Append(string.Format(" use_projectname='{0}'", projectName));
                lcs.BLL.out_storageList obll = new BLL.out_storageList();
                ds= obll.GetListVn(strWhere.ToString());                
            }
            return ds;
        }

        public void BindData()
        {
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                int projectId = (Convert.ToInt32(Request.Params["id"]));

                Maticsoft.BLL.project bll = new Maticsoft.BLL.project();
                Maticsoft.Model.project model = bll.GetModel(projectId);
                string projectName = model.projectName;
                lbl_projectname.Text = projectName;                
                StringBuilder strWhere = new StringBuilder();
                if (txtKeyword.Text.Trim() != "")
                {
                    strWhere.AppendFormat(" and material like '%{0}%'", txtKeyword.Text.Trim());
                }
                if (strWhere.ToString() != "")
                    strWhere.Append(string.Format(" and target_project='{0}'", projectName));
                else
                    strWhere.Append(string.Format(" and target_project='{0}'", projectName));
                lcs.BLL.out_storageList obll = new BLL.out_storageList();
                DataSet ds = obll.GetListVn(strWhere.ToString());
                this.GridView2.DataSource = ds;
                this.GridView2.DataBind();
            }
        }
        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView2.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                //e.Row.Cells[0].Text = "<input id='Checkbox2' type='checkbox' onclick='CheckAll()'/><label></label>";
            }
        }
         protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行
            {
                //当鼠标放上去的时候 先保存当前行的背景颜色 并设置新的背景色
                e.Row.Attributes.Add("onmouseover", "onmouseroverop(this)");
                //当鼠标离开的时候 将背景颜色恢复成之前的颜色
                e.Row.Attributes.Add("onmouseout", "onmouseroutop(this)");
                //单击行改变行背景颜色
                e.Row.Attributes.Add("onclick", "ItemOver(this)");
            }
        }        
       }
}